package com.example.vueadmin.service;

import com.example.vueadmin.entity.Enter;
import com.example.vueadmin.entity.Finance;
import com.example.vueadmin.entity.Outer;
import com.example.vueadmin.mapper.InventoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Service
public class FinanceService {
    @Autowired
    private InventoryMapper inventoryMapper;


    public Finance getErp() {
        /*
         * 利润 = 入库价-出货价
         * 入库价 = 总入库数量 * 进货价
         * 出货价 = 总出货 * 出货价
         * */
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.setTime(new Date());
        Finance data = new Finance();
        Integer enterCount = 0;
        Integer outerCount = 0;
        Integer enterPrice = 0;
        Integer outerPrice = 0;
        Integer moonEnterPrice = 0;
        Integer moonOuterPrice = 0;
        List<Enter> enterList = inventoryMapper.queryEnter();
        List<Outer> outerList = inventoryMapper.queryOuter();
        //日入库判断
        for (int i = 0; i < enterList.size(); i++) {
            cal2.setTime(enterList.get(i).getEnterTime());
            if (cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR)) {
                enterCount += enterList.get(i).getCount();
                enterPrice += enterList.get(i).getCount() * enterList.get(i).getCost();
            }
            if (cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH)) {
                moonEnterPrice += enterList.get(i).getCount() * enterList.get(i).getCost();
            }
        }
        //日出库判断
        for (int j = 0; j < outerList.size(); j++) {
            cal2.setTime(outerList.get(j).getOuterTime());
            if (cal2.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR)) {
                outerCount += outerList.get(j).getCount();
                outerPrice += outerList.get(j).getCount() * outerList.get(j).getSale();
            }
            if (cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH)) {
                moonOuterPrice += outerList.get(j).getCount() * outerList.get(j).getSale();
            }
        }
        data.setDayEnter(enterCount);
        data.setDayOuter(outerCount);
        data.setDayProfit(outerPrice - enterPrice);
        data.setMoonProfit(moonOuterPrice - moonEnterPrice);
        return data;
    }
}
